﻿
/// <summary>
/// *** wangyel1 ***
/// 魔方整体的转动用摄像机的转动来模拟，这里用了一个中心体带动相机转动
/// </summary>

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;

public class TouchRotate : MonoBehaviour
{

    public Transform cameraTarget;  //摄像机对象
    public Transform directionalLight;  //灯光对象
    public float horizontalSpeed = 2.0f; //水平滑动速度//
    public float verticalSpeed = 2.0f; //竖直滑动速度//
    public float scrollSpeed = 2.0f; //滚轮滚动速度//
    public float lerpSpeed = 5.0f; //插值速度//
    private float h = 0;
    private float v = 0;
    private float caremaDistance;

    //private bool isOneFingerRotate;

    public Button revertCameraButton;
    //储存摄像机初始位置
    private float startCameraPosX;
    private float startCameraPosY;
    private float startCameraPosZ;
    //储存摄像机初始角度
    private float startCameraRotationX;
    private float startCameraRotationY;
    private float startCameraRotationZ;

    //储存灯光初始位置
    private float startLightPosX;
    private float startLightPosY;
    private float startLightPosZ;
    //储存灯光初始角度
    private float startLightRotationX;
    private float startLightRotationY;
    private float startLightRotationZ;

    //储存GameCenter初始位置
    private float startGameCenterPosX;
    private float startGameCenterPosY;
    private float startGameCenterPosZ;

    //储存GameCenter初始角度
    private float startGameCenterRotationX;
    private float startGameCenterRotationY;
    private float startGameCenterRotationZ;

    Quaternion gameCenterRotation;

    //记录上一次触摸位置,判断是放大还是缩小手势
    private Vector3 oldPos1;
    private Vector3 oldPos2;

    void Start()
    {
        //获取摄像机初始位置
        startCameraPosX = cameraTarget.transform.position.x;
        startCameraPosY = cameraTarget.transform.position.y;
        startCameraPosZ = cameraTarget.transform.position.z;

        //获取摄像机初始角度
        Vector3 localRotation = transform.eulerAngles;

        startCameraRotationX = localRotation.x;
        startCameraRotationY = localRotation.y;
        startCameraRotationZ = localRotation.z;

        //获取灯光初始位置
        startLightPosX = directionalLight.position.x;
        startLightPosY = directionalLight.position.y;
        startLightPosZ = directionalLight.position.z;

        //获取灯光初始角度
        startLightRotationX = localRotation.x;
        startLightRotationY = localRotation.y;
        startLightRotationZ = localRotation.z;

        //获取GameCenter初始位置
        startGameCenterPosX = transform.position.x;
        startGameCenterPosY = transform.position.y;
        startGameCenterPosZ = transform.position.z;

        //获取GameCenter初始角度
        startGameCenterRotationX = localRotation.x;
        Debug.Log(startGameCenterRotationX);
        startGameCenterRotationY = localRotation.y;
        startGameCenterRotationZ = localRotation.z;

        //startGameCenterRotationX = transform.localEulerAngles.x;
        //startGameCenterRotationY = transform.localEulerAngles.y;
        //startGameCenterRotationZ = transform.localEulerAngles.z;
        
        //gameCenterRotation = transform.localRotation;

        v = localRotation.x;
        h = localRotation.y;
        
    }

    void Update()
    {
        //在电脑上鼠标左键旋转摄像机
        if (Input.GetMouseButton(0))
        {
            h += horizontalSpeed * Input.GetAxis("Mouse X");
            v -= verticalSpeed * Input.GetAxis("Mouse Y");
            v = Mathf.Clamp(v, -50, 50);
        }

        //在手机上一只手指头旋转摄像机(判断单点触摸)
        if (Input.touchCount == 1)
        {
            //判断触摸类型为移动触摸
            if (Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                h += horizontalSpeed * Input.GetAxis("Mouse X");
                v -= verticalSpeed * Input.GetAxis("Mouse Y");
                v = Mathf.Clamp(v, -50, 50);
            }
        }

        Quaternion rotationTo = Quaternion.Euler(v, h, 0);
        transform.rotation = Quaternion.Lerp(transform.rotation, rotationTo, Time.deltaTime * lerpSpeed);

        //在电脑上用滚轮控制放大缩小
        //caremaDistance += Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
        if(Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            caremaDistance += Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
            caremaDistance = Mathf.Clamp(caremaDistance, -4, 1);
            cameraTarget.localPosition = Vector3.Lerp(cameraTarget.localPosition, new Vector3(0, 0, caremaDistance - 3), Time.deltaTime * lerpSpeed);
        }
        //在手机上多点触摸控制放大缩小
        if (Input.touchCount > 1)
        {
            //前两只手指为移动触摸
            if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
            {
                //计算移动触摸两点当前触摸位置
                Vector3 tempPos01 = Input.GetTouch(0).position;
                Vector3 tempPos02 = Input.GetTouch(1).position;
                //函数返回真(放大),返回假(缩小)
                if (isEnlarge(oldPos1, oldPos2, tempPos01, tempPos02))
                {
                    caremaDistance = Mathf.Clamp(caremaDistance, 0, 1);
                    cameraTarget.localPosition = Vector3.Lerp(cameraTarget.localPosition, new Vector3(0, 0, caremaDistance - 3), Time.deltaTime * lerpSpeed);
                }
                else
                {
                    caremaDistance = Mathf.Clamp(caremaDistance, -4, 0);
                    cameraTarget.localPosition = Vector3.Lerp(cameraTarget.localPosition, new Vector3(0, 0, caremaDistance - 3), Time.deltaTime * lerpSpeed);
                }

                //备份上一次触摸点的位置，用于对比
                oldPos1 = tempPos01;
                oldPos2 = tempPos02;
                
            }
        }
        
    }

    /// <summary>
    /// 传入两次手势位置,计算放大还是缩小
    /// </summary>
    /// <param name="oldPos1">上次触摸位置01</param>
    /// <param name="oldPos2">上次触摸位置02</param>
    /// <param name="currPos1">本次触摸位置01</param>
    /// <param name="currPos2">本次触摸位置02</param>
    /// <returns></returns>
    bool isEnlarge(Vector3 oldPos1, Vector3 oldPos2, Vector3 currPos1, Vector3 currPos2)
    {
        //计算用户手势
        float length1 = Mathf.Sqrt((oldPos1.x - oldPos2.x) * (oldPos1.x - oldPos2.x) + (oldPos1.y - oldPos2.y) * (oldPos1.y - oldPos2.y));
        float length2 = Mathf.Sqrt((currPos1.x - currPos2.x) * (currPos1.x - currPos2.x) + (currPos1.y - currPos2.y) * (currPos1.y - currPos2.y));
        if (length1 < length2)
        {
            //放大
            return true;
        }
        else
        {
            //缩小
            return false;
        }
    }

    /// <summary>
    /// 重置游戏视角
    /// </summary>
    public void RevertGamePosition()
    {
        //恢复摄像机位置
        cameraTarget.position = new Vector3(startCameraPosX, startCameraPosY, startCameraPosZ);
        cameraTarget.eulerAngles = new Vector3(startCameraRotationX, startCameraRotationY, startCameraRotationZ);

        //恢复灯光位置
        directionalLight.position = new Vector3(startLightPosX, startLightPosY, startCameraPosZ);
        directionalLight.eulerAngles = new Vector3(startLightRotationX, startLightRotationY, startLightRotationZ);

        //恢复GameCenter位置
        transform.position = new Vector3(startGameCenterPosX, startGameCenterPosY, startGameCenterPosZ);
        //transform.eulerAngles = new Vector3(startGameCenterRotationX, startGameCenterRotationY, startGameCenterRotationZ);
        // transform.localRotation.eulerAngles = gameCenterRotation;

        Quaternion rotationTo = Quaternion.Euler(startCameraRotationX, startCameraRotationY, startCameraRotationZ);
        transform.rotation = Quaternion.Lerp(transform.rotation, rotationTo, Time.deltaTime * lerpSpeed);
    }



    #region 重置摄像机的位置
    ///// <summary>
    ///// update方法调用结束后进入,算出重置摄像机的位置
    ///// </summary>
    //void LateUpdate()
    //{

    //}
    #endregion
}
